﻿using BizBrowser.RPT.PrintEntity;
using FastReport;
using FastReport.Data;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace BizBrowser.RPT
{
    public partial class Design : Form
    {
        string baseDir = Path.Combine(Application.StartupPath, "reports");
        Report report = new Report();
        FileStream fs = null;
        String rptPath;
        String fileName;
        public Design(String fileName)
        {
            InitializeComponent();
            if(fileName != null)
            {
                this.rptPath = Path.Combine(baseDir, fileName);
                this.fileName = fileName;
            }
        }

        private void Design_Load(object sender, EventArgs e)
        {
            FastReport.Utils.Res.LoadLocale(Application.StartupPath+"/cn.frl");
            if (fileName != null && !"".Equals(fileName))
            {
                bool exists = File.Exists(rptPath);
                if (exists)
                {
                    this.Content.cmdSave.CustomAction += new EventHandler(SaveReport);
                    this.Content.AskSave = false;
                    fs = new FileStream(rptPath, FileMode.Open, FileAccess.ReadWrite);
                    report.Load(fs);
                    fs.Close();
                }

            }
            registerRptHeaderFooter(report);
            registerDetail(report); 
            this.Content.Report = report;
            this.Content.RefreshLayout();
        }

        void SaveReport(object sender,System.EventArgs args)
        {
            try
            {
                FileStream fs = new FileStream(rptPath, FileMode.Open, FileAccess.ReadWrite);
                report.Save(fs);
                fs.Close();
            }catch(Exception ex)
            {
                Console.WriteLine(ex);
                MessageBox.Show("保存失败");
                return;
            }
            MessageBox.Show("保存成功");
        }


        void registerRptHeaderFooter(Report report)
        {
            RptHeaderFooter hf = new RptHeaderFooter();          
            List<RptHeaderFooter> l = new List<RptHeaderFooter>();
            l.Add(hf);
            report.RegisterData(l, Constant.H_F_NAME);
        }


        void registerDetail(Report report)
        {
            RptDetail detail = new RptDetail();
            List<RptDetail> l = new List<RptDetail>();
            l.Add(detail);
            report.RegisterData(l, Constant.DETAIL_NAME);
        }

    }
}
